package com.jacen.fzxs.manager.task;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SystemInitListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent event) {
		// TODO Auto-generated method stub
		ServletContext servletContext = event.getServletContext();
		WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		List<ISystemInitTask> taskList = ((SystemInitTaskBean) springContext.getBean("systemInitTaskBean")).getTaskList();
		ISystemInitTask task;
		for (int i = 0; i < taskList.size(); i++) {
			task = taskList.get(i);
			if (task.onSysInit(servletContext, springContext)!= ISystemInitTask.SUCCESS) {
				System.out.println("Init system failure by: " + task.getClass().getName());
				for (int j = i; j >= 0; j--) {
					task = taskList.get(j);
					task.onSysDestroy(servletContext, springContext);
				}
				System.exit(0);
			}
		}
		System.out.println("Completed system iniInitializ.");
		
	}

	@Override
	public void contextDestroyed(ServletContextEvent event) {
		// TODO Auto-generated method stub
		
	}



}
